package tests

import (
	"github.com/astaxie/beego/orm"
	"time"
	"luckapi/models"
	"luckapi/mutils"
)

func TestSql(){
	models.GetJoinLuckNumByGameId(2)
	//addTables()
	//addUsers()
	//addJoiners()
}

func addTables()  {

	orm.Debug = true
	// 自动建表
	orm.RunSyncdb("default", false, true)

	t := time.Now().Unix()
	tm := time.Unix(t, 0)
	timestr := tm.Format("2006-01-02 03:04:05 PM")

	game := new(models.Game)
	game.Star_time = timestr
	game.End_time = timestr
	game.Money = 14000
	game.Topic = "微信推出新功能：可双向删除好友。你喜欢这个新功能吗？"
	game.Join_gold = 100
	game.Game_num = "004"
	game.Game_level = 4
	game.Status = 3
	game.Luck_gold = 5000
	game.Luck_time = timestr

	result, message := models.AddGame(game)
	if !result {
		println(message)
	}

	game1 := new(models.Game)
	game1.Star_time = timestr
	game1.End_time = timestr
	game1.Money = 11000
	game1.Topic = "微信推出新功能：可双向删除好友。你喜欢这个新功能吗？"
	game1.Join_gold = 100
	game1.Game_num = "004"
	game1.Game_level = 1
	game1.Status = 3
	game1.Luck_gold = 5000
	game1.Luck_time = timestr

	result, message = models.AddGame(game1)
	if !result {
		println(message)
	}

	game2 := new(models.Game)
	game2.Star_time = timestr
	game2.End_time = timestr
	game2.Money = 12000
	game2.Topic = "微信推出新功能：可双向删除好友。你喜欢这个新功能吗？"
	game2.Join_gold = 100
	game2.Game_num = "004"
	game2.Game_level = 2
	game2.Status = 3
	game2.Luck_gold = 5000
	game2.Luck_time = timestr

	result, message = models.AddGame(game2)
	if !result {
		println(message)
	}

	game3 := new(models.Game)
	game3.End_time = timestr
	game3.Money = 13000
	game3.Topic = "微信推出新功能：可双向删除好友。你喜欢这个新功能吗？"
	game3.Join_gold = 100
	game3.Game_num = "004"
	game3.Game_level = 3
	game3.Status = 3
	game3.Luck_gold = 5000
	game3.Luck_time = timestr

	result, message = models.AddGame(game3)
	if !result {
		println(message)
	}

	/*
	o := orm.NewOrm()
	o.Using("default")
	game := models.Game{Id:2}
	err, joiners := models.GetJoinerListByGameId(game)
	if err == nil {
		mutils.MLogs(joiners)
	}

	err1 ,games := models.GetGameListOpen()
	if err1 == nil {
		mutils.MLogs(games)
	}

	t := time.Now().Unix()
	tm := time.Unix(t, 0)
	timestr := tm.Format("2006-01-02 03:04:05 PM")


	user := new(models.User)
	user.UserId = mutils.GetUserIdOnly()
	user.Balance = 1001230
	user.Name = "测试用户3"
	user.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
	user.Create_time = timestr
	user.Phone = "18017205591"
	user.Password = "1234567"

	result, message := models.AddNewPerson(user)
	if !result {
		println(message)
	}


	t := time.Now().Unix()
	tm := time.Unix(t, 0)
	timestr := tm.Format("2006-01-02 03:04:05 PM")
	game := new(models.Game)
	game.Star_time = timestr
	game.End_time = timestr
	game.Money = 6800
	game.Topic = "微信推出新功能：可双向删除好友。你喜欢这个新功能吗？"
	game.Join_gold = 100
	game.Game_num = "004"
	game.Game_level = 4
	game.Status = 3
	game.Luck_gold = 5000
	game.Luck_time = timestr

	result, message := models.AddGame(game)
	if !result {
		println(message)
	}
	*/
		/*
		err ,games := models.GetGameListOpen()
		if err != nil {
			println("error:",err)
		}
		fmt.Println(games)

		/*
		user := new(models.User)
		user.UserId = mutils.GetUserIdOnly()
		user.Balance = 1001230
		user.Name = "测试用户3"
		user.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
		user.Create_time = timestr
		user.Phone = "18017205591"
		user.Password = "1234567"
		o.Insert(user)

		game := models.Game{Id:2}
		user := models.User{Id:6}
		joiner := models.Joiner{}
		 joiner.Game = &game
		 joiner.User = &user
		 joiner.Luck_num = "003"
		 joiner.Datetime = timestr
		o.Insert(&joiner)

		/*
		game :=  models.Game{Id:2}
		game.Game_level = 1
		game.Status = 3
		game.Luck_gold = 5000
		game.Luck_num = "003"
		game.Luck_time = timestr
		o.Update(&game,"game_level","status","luck_gold","luck_num","luck_time")



		joiner := models.Joiner{Id:1}
		joiner.Luck_num = "003"
		o.Update(&joiner,"luck_number")
		*/
}

func addUsers()  {
	t := time.Now().Unix()
	tm := time.Unix(t, 0)
	timestr := tm.Format("2006-01-02 03:04:05 PM")
	user := new(models.User)
	user.UserId = mutils.GetUserIdOnly()
	user.Balance = 11111
	user.Name = "测试用户1"
	user.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
	user.Create_time = timestr
	user.Phone = "18017205591"
	user.Password = "123456"
	orm.NewOrm().Insert(user)

	user1 := new(models.User)
	user1.UserId = mutils.GetUserIdOnly()
	user1.Balance = 22222
	user1.Name = "测试用户2"
	user1.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
	user1.Create_time = timestr
	user1.Phone = "18019950499"
	user1.Password = "1234567"
	orm.NewOrm().Insert(user1)

	user2 := new(models.User)
	user2.UserId = mutils.GetUserIdOnly()
	user2.Balance = 333333
	user2.Name = "测试用户3"
	user2.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
	user2.Create_time = timestr
	user2.Phone = "18019950498"
	user2.Password = "12345678"
	orm.NewOrm().Insert(user2)

	user3 := new(models.User)
	user3.UserId = mutils.GetUserIdOnly()
	user3.Balance = 44444
	user3.Name = "测试用户4"
	user3.Avatar = "http://q4.qlogo.cn/g?b=qq&nk=485463145&s=100"
	user3.Create_time = timestr
	user3.Phone = "18019950497"
	user3.Password = "123456789"
	orm.NewOrm().Insert(user3)
}

func addJoiners()  {
	t := time.Now().Unix()
	tm := time.Unix(t, 0)
	timestr := tm.Format("2006-01-02 03:04:05 PM")
	game := models.Game{Game_level:1}
	user := models.User{Id:1}
	joiner := models.Joiner{}
	joiner.Game = &game
	joiner.User = &user
	joiner.Luck_num = "003"
	joiner.Datetime = timestr
	orm.NewOrm().Insert(&joiner)


	game1 := models.Game{Game_level:1}
	user1 := models.User{Id:3}
	joiner1 := models.Joiner{}
	joiner1.Game = &game1
	joiner1.User = &user1
	joiner1.Luck_num = "004"
	joiner1.Datetime = timestr
	orm.NewOrm().Insert(&joiner1)

	game2 := models.Game{Game_level:1}
	user2 := models.User{Id:4}
	joiner2 := models.Joiner{}
	joiner2.Game = &game2
	joiner2.User = &user2
	joiner2.Luck_num = "004"
	joiner2.Datetime = timestr
	orm.NewOrm().Insert(&joiner2)

	user3 := models.User{Id:4}
	joiner3 := models.Joiner{}
	joiner3.Game = &game2
	joiner3.User = &user3
	joiner3.Luck_num = "004"
	joiner3.Datetime = timestr
	orm.NewOrm().Insert(&joiner3)
}